// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Jeff Stewart
// Reads externalized registry XML blobs that Vista uses with Windows Mail.

#ifndef POPDIAG_CONFIG_XMLREGISTRYKEY_H__
#define POPDIAG_CONFIG_XMLREGISTRYKEY_H__

#include <QtCore/QList>
#include <QtCore/QString>
#include <QtXml/QDomNode>

#include "config/registrykey.h"

namespace popdiag {

class XmlRegistryKey : public RegistryKey {
 public:
  // Create an XmlRegistryKey wrapping data found in file at path.
  explicit XmlRegistryKey(const QString &path);

  // All these methods match the corresponding RegistryKey versions.
  // See registrykey.h for descriptions:
  ~XmlRegistryKey();

  bool GetChildKeyNames(QList<QString> *out) const;
  RegistryKey *ChildKey(const QString &sub_key_name) const;

  bool ReadIntEntry(const QString &value_name, int *out) const;
  bool ReadStringEntry(const QString &value_name, QString *out) const;
  bool ReadBinaryString(const QString &value_name, QString *out) const;
  unsigned char *ReadBinaryEntry(const QString &value_name,
                                 size_t *out_len_in_bytes) const;

  QString Dump() const;
  RegistryKey *Copy() const;
  void Refresh();

 private:

  // Find a child node named value_name, and return it in out if the type of
  // the node is required_type.  Return true iff this succeeds and out is
  // populated.
  bool GetRelevantNode(const QString &value_name, const char *required_type,
		       QDomNode *out) const;

  // Retrieves the string representation of the data type stored in this node.
  // Known values seen so far include SZ (string), DWORD (double word),
  // and BINARY
  QString GetNodeType(const QDomNode &node) const;

  // Fetch the node for an attributes named value_name.
  QDomNode GetNamedChild(const QString &name) const;

  scoped_ptr<QDomNode> node_;
  QString path_;
  DISALLOW_EVIL_CONSTRUCTORS(XmlRegistryKey);
};

}  // namespace popdiag

#endif  // POPDIAG_CONFIG_XMLREGISTRYKEY_H__
